package com.zzb.framework.config;

import com.alibaba.druid.util.JdbcUtils;
import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.incrementer.OracleKeyGenerator;
import com.baomidou.mybatisplus.incrementer.PostgreKeyGenerator;
import com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.plugins.parser.ISqlParser;
import com.baomidou.mybatisplus.plugins.parser.ISqlParserFilter;
import com.baomidou.mybatisplus.plugins.parser.tenant.TenantHandler;
import com.baomidou.mybatisplus.plugins.parser.tenant.TenantSqlParser;
import com.baomidou.mybatisplus.toolkit.PluginUtils;
import com.zzb.framework.mybatisplus.incrementer.MysqlKeyGenerator;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangzhenbin on 18-1-17.
 */
@Configuration
@MapperScan("com.zzb.framework.**.dao.mapper*")
public class MybatisPlusConfig {
    @Value("${spring.database.type:mysql}")
    private String databaseType;

    /**
     * mapper-plus SQL执行效率插件【生产环境可以关闭】
     */
    @Bean
    public PerformanceInterceptor performanceInterceptor() {
        return new PerformanceInterceptor();
    }

    /**
     * mapper-plus分页插件<br>
     * 文档：http://mp.baomidou.com<br>
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setLocalPage(true);// 开启 PageHelper 的支持
        return paginationInterceptor;
    }

    @Bean
    public GlobalConfiguration globalConfiguration() {
        GlobalConfiguration conf = new GlobalConfiguration();
        if(databaseType.equalsIgnoreCase(JdbcUtils.MYSQL)){
            conf.setKeyGenerator(new MysqlKeyGenerator());
        } else if(databaseType.equalsIgnoreCase(JdbcUtils.POSTGRESQL)){
            conf.setKeyGenerator(new PostgreKeyGenerator());
        } else if(databaseType.equalsIgnoreCase(JdbcUtils.ORACLE)){
            conf.setKeyGenerator(new OracleKeyGenerator());
        }
        return conf;
    }
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor(){
        return new OptimisticLockerInterceptor();
    }
}